/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.            */
/*                                                                 */
/*  NAME      = t_sqlserver_detect.c                               */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/20 by geyh                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>

#include <frcomm.h>
#include <sqlserver/config.h>
#include <sqlserver/sqlserver_core.h>
#include <apmtest_comm.h>
#include "../../test_sqlserver_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00", 1, TEST_SQLSERVER_SRC_IP, TEST_SQLSERVER_DST_IP,
                                      TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

static void __case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x00\x00\x00\x00\x00\x00", 7, TEST_SQLSERVER_SRC_IP,
                                      TEST_SQLSERVER_DST_IP, TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT,
                                      st_timestamp), FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

static void __case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x05\x00\x00\x00\x00\x00\x00\x00", 8,
                                      TEST_SQLSERVER_SRC_IP,
                                      TEST_SQLSERVER_DST_IP, TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT,
                                      st_timestamp),
            FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

static void __case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x04\x04\x00\x00\x00\x00\x00\x00", 8,
                                      TEST_SQLSERVER_SRC_IP,
                                      TEST_SQLSERVER_DST_IP, TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT,
                                      st_timestamp),
            FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

static void __case5(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x04\x00\x00\x08\x00\x00\x00\x00\x00", 9,
                                      TEST_SQLSERVER_SRC_IP,
                                      TEST_SQLSERVER_DST_IP, TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT,
                                      st_timestamp),
            FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

static void __case6(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x04\x00\x00\x08\x00\x00\x00\x00", 8,
                                      TEST_SQLSERVER_SRC_IP,
                                      TEST_SQLSERVER_DST_IP, TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT,
                                      st_timestamp),
            FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

static void __case7(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x04\x00\x00\x09\x00\x00\x00\x00", 8,
                                      TEST_SQLSERVER_SRC_IP,
                                      TEST_SQLSERVER_DST_IP, TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT,
                                      st_timestamp),
            FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

static void __case8(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *) frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x04\x00\x00\x09\x00\x00\x00\x01", 8,
                                      TEST_SQLSERVER_SRC_IP,
                                      TEST_SQLSERVER_DST_IP, TEST_SQLSERVER_SRC_PORT, TEST_SQLSERVER_DST_PORT,
                                      st_timestamp),
            FR_SUCC);

    i_ret = sqlserver_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    frfree(p_st_conn);

    return;
}

int add_suite_sqlserver_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" sqlserver_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL ||
        CU_add_test(pSuite, "case8", __case8) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
